<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/12/14
 * Time: 10:37
 */

namespace app\api\controller;

use app\api\model\Area AS AreaModel;

class Area extends Base
{
    /**
     * 获取地址
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getArea(){
        header("Access-Control-Allow-Origin:*");
        $AREA = new AreaModel();
        $province = $AREA::selectArea(['level' => 1], 'district_id, district');
        $city = $AREA::selectArea(['level' => 2], 'district_id, district, pid');
        $district = $AREA::selectArea(['level' => 3], 'district_id, district, pid');
        foreach ($city as $kc => $vc){
            foreach ($district as $kd => $vd){
                if($vd['pid'] == $vc['district_id']){
                    $city[$kc]['items'][] = $vd;
                }
            }
        }
        foreach ($province as $kp => $vp){
            foreach ($city as $k => $v){
                if($v['pid'] == $vp['district_id']){
                    $province[$kp]['items'][] = $v;
                }
            }
        }
        return json(['status' => 200, 'msg' => '', 'data' => $province]);
    }
}